let access_database = require("../database/access_database")
let staffSQLStore = require("../database/SQLSTORE_staff")
let a = require("fs")
let message = require("./message")
let code_message = require("./JsonFileOprate")
let crypto = require("crypto")
let secret = "aoligei"
module.exports = {

    "staffSignUp": async function (req, res) {
        console.log(code_message)
        let fs = require("fs")
        if (!req.body.name) {
            res.json(new message(443, null, code_message["443"]))
        } else if (req.body.name.length > 50) {
            res.json(new message(444, null, code_message["444"]))
        } else {
            //first confirt this is a new email
            try {
                let nums = await access_database(staffSQLStore.get("GetSameEmailNums"), [req.body.email]);
                if (nums[0].same_email_nums > 0) {
                    res.json(new message(446, null, code_message["446"]))
                } else {
                    if (req.body.email.length > 50) {
                        res.json(new message(445, null, code_message["445"]))

                    } else {
                        let generateID = Math.floor(Math.random() * 10000 + 10001)

                        let secret = "aoligei"
                        const hash = crypto.createHmac('sha256', secret).update(req.body.pwd).digest('hex')
                        access_database(staffSQLStore.get("InsertStaff"),
                            [generateID, req.body.name, req.body.email, hash])
                            .then((results) => {
                                if (results.affectedRows > 0) {
                                    res.json(new message(201, null, eval(code_message['201'])))
                                } else {
                                    res.json(new message(502, null, code_message["502"]))
                                }

                            }).catch((error) => {
                            res.json(new message(501, null, error))
                        })
                    }
                }
            } catch (e) {
                res.json(new message(447, null, code_message["447"] + JSON.stringify(e)))
            }


        }
    },
    "staffIDSignIn": async function (req, res) {
        if (req.body.staffid) {
            if (req.body.password) {
                try {
                    let results = await access_database(staffSQLStore.get("GetStaffPasswordByStaffID"), [req.body.staffid])
                    console.log(results)
                    let inputPasswordCripto = crypto.createHmac('sha256', secret).update(req.body.password).digest('hex')
                    if (results.length>0) {
                        if (inputPasswordCripto == results[0].password) {
                            req.session.staffid = req.body.staffid

                            let results = await access_database(staffSQLStore.get("GetStaffNameByStaffEmail"), [req.body.staffid])
                            let name = results[0].name
                            req.session.staffname = name
                            res.json(new message(207, {"staffname": name}, eval(code_message["207"])))
                        } else {
                            res.json(new message(206, null, code_message["206"]))
                        }
                    }


                    else {
                        res.json(new message(205, null, code_message["205"]))
                    }
                } catch (e) {
                    console.log(e)
                    res.json(new message(204, null, code_message["204"]))
                }
            } else {
                res.json(new message(203, null, code_message["203"]))
            }
        } else {
            res.json(new message(202, null, code_message["202"]))

        }


    },
    "staffEmailSignIn": async function (req, res) {
        let staffid
        if (!req.body.staffemail) {
            res.json(new message(209, null, code_message["209"]))
        }
        {
            results = await access_database(staffSQLStore.get("GetStaffIDByStaffEmail"), [req.body.staffemail])
            if (results.length > 0) {
                staffid = results[0].id
            } else {
                res.json(new message(208, null, code_message["208"]))
            }

        }
        if (staffid) {
            if (req.body.password) {
                try {
                    let results = await access_database(staffSQLStore.get("GetStaffPasswordByStaffID"), [staffid])
                    console.log(results)
                    let inputPasswordCripto = crypto.createHmac('sha256', secret).update(req.body.password).digest('hex')
                    if (results[0]) {
                        if (inputPasswordCripto == results[0].password) {
                            req.session.staffid = staffid
                            let results = await access_database(staffSQLStore.get("GetStaffNameByStaffEmail"), [staffid])
                            let name = results[0].name
                            req.session.staffname = name
                            res.json(new message(207, {"staffname": name}, eval(code_message["207"])))
                        } else {
                            res.json(new message(206, null, code_message["206"]))
                        }
                    } else {
                        res.json(new message(205, null, code_message["205"]))
                    }
                } catch (e) {
                    console.log(e)
                    res.json(new message(204, null, code_message["204"]))
                }
            } else {
                res.json(new message(203, null, code_message["203"]))
            }
        } else {
            res.json(new message(202, null, code_message["202"]))

        }
    },
    "staffLogOut": function (req, res) {
        if (req.session.staffid) {
            req.session.destroy()
            res.json(new message(210, null, code_message["210"]))
        } else {
            res.json(new message(449, null, code_message["449"]))
        }
    }


}